package cn.kgc.controller;

import cn.kgc.pojo.UserRole;
import cn.kgc.service.userRoleService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @author: admin
 * @date: 2022/2/17
 * @description:
 */
@Controller
@RequestMapping("/lq")
public class UserRoleConteoller {

    @Resource
    private userRoleService ur;
    //添加用户
    @RequestMapping("/addRole")
    public String addRole(){

        return "添加用户信息";
    }
    //模糊分页查询
    @RequestMapping("/likeRole")
    @ResponseBody
    public String likeRole(@RequestParam String name, @RequestParam String role, @RequestParam String department,@RequestParam Integer currentPage,@RequestParam Integer pageSize){
        PageHelper.startPage(currentPage,pageSize);
        Page<UserRole> page=ur.getList(name,role,department);
        System.out.println("role = " + role);
        System.out.println("department = " + department);
        System.out.println("name = " + name);
        PageInfo<UserRole> pageInfo = new  PageInfo<UserRole>(page);
        return JSON.toJSONString(pageInfo);
    }

    @RequestMapping("/userRole")
    public String goUser(){
        return "用户管理";
    }
    //修改
    @RequestMapping("/修改用户信息")
    public String updRole(){

        return "修改用户信息";
    }

    //删除
    @RequestMapping("/delRole")
    @ResponseBody
    public String delRole(@RequestParam Integer id){
        boolean bo =ur.del(id);
        return JSON.toJSONString(bo);
    }
    //添加
    @RequestMapping("/add")
    @ResponseBody
    public String add(@RequestParam String coding,@RequestParam String name,@RequestParam String password,@RequestParam String role,@RequestParam String department,@RequestParam String phone){
       UserRole role1 = new UserRole();
        role1.setCoding(coding);
        role1.setName(name);
        role1.setPassword(password);
        role1.setRole(role);
        role1.setDepartment(department);
        role1.setPhone(phone);
        boolean bo = ur.add(role1);
        System.out.println("================================"+bo);
        return JSON.toJSONString(bo);
    }
    //修改
    @RequestMapping("/upd")
    @ResponseBody
    public String upd(@RequestParam String coding,@RequestParam String name,@RequestParam String password,@RequestParam String role,@RequestParam String department,@RequestParam String phone,@RequestParam int id){
       UserRole role2 = new UserRole();
       role2.setId(id);
       role2.setCoding(coding);
       role2.setName(name);
       role2.setPassword(password);
       role2.setRole(role);
       role2.setDepartment(department);
       role2.setPhone(phone);
        System.out.println("====================="+role2.toString());
       boolean bo1 = ur.upd(role2);
        return JSON.toJSONString(bo1);
    }
    //传值
    @RequestMapping("/selById2/{id}")
    public String ById2(Model model,@PathVariable int id){
        UserRole userrole = ur.selRoleById(id);
        model.addAttribute("userRole",userrole);
        return "修改用户信息";
    }


    //传值
    @RequestMapping("/selById/{id}")
    public String ById(Model model,@PathVariable int id){
        UserRole userrole = ur.selRoleById(id);
        model.addAttribute("userRole",userrole);
        return "查看用户明细";
    }


    @RequestMapping("/selRole")
    public String selRole(){

        return "查看用户明细";
    }
}
